package com.wanmait.hotelManage.webservlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.wanmait.hotelManage.dao.CommentDAO;
import com.wanmait.hotelManage.dao.RoomTypeCommentDAO;
import com.wanmait.hotelManage.dao.RoomTypeDAO;
import com.wanmait.hotelManage.dao.RoomimagesDAO;
import com.wanmait.hotelManage.vo.RoomType;
import com.wanmait.hotelManage.vo.RoomTypeComment;
import com.wanmait.hotelManage.vo.Roomimages;
import com.wanmait.hotelManage.vo.Userinfo;

/**
 * Servlet implementation class commentServerlet
 */
@WebServlet("/commentServerlet")
public class commentServerlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public commentServerlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	 String action=request.getParameter("action");
	 if("comment".equals(action)) {
		 this.comment(request, response);
	 }
	 else if("hotecomment".equals(action)) {
		 this.hoteComment(request, response);
	 }
	 else if("pan".equals(action)) {
		 this.pan(request, response);
	 }
	}
	protected void pan(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
	{
		
		PrintWriter print = response.getWriter();
		Userinfo userinfo=(Userinfo)request.getSession().getAttribute("userinfo");
		boolean b=CommentDAO.getCommentDAO().pan(userinfo);
		if(b) {
			print.print("ok");
		}
		else {
			print.print("no");
		}
		
	}
	protected void hoteComment(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
	{
		
		System.out.println(0000);
		int userinfoId=Integer.parseInt(request.getParameter("userinfoid"));
		  String Content=request.getParameter("hotecomment");
		  CommentDAO.getCommentDAO().addHoteComment(userinfoId, Content, 0);
		  response.sendRedirect(request.getContextPath()+"/CommentServlet?action=list");
	}
	protected void comment(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
	{
		int roomTypeId=Integer.parseInt(request.getParameter("roomtypeid"));
		  int userinfoId=Integer.parseInt(request.getParameter("userinfoid"));
		
		  String Content=request.getParameter("content");
		  CommentDAO.getCommentDAO().addComment(roomTypeId, userinfoId, Content, 0);
		String roomTypeTitle=  request.getParameter("roomtypetitle");
		String roomTypeName=request.getParameter("roomTypeName");
		String startTime=request.getParameter("startTime");
		String endTime=request.getParameter("endTime");
	    RoomType roomType=	(RoomType)RoomTypeDAO.getRoomtypedao().roomTypeDetalis(roomTypeName);
	    List<RoomType> roomTypes = RoomTypeDAO.getRoomtypedao().findAll(); 
		request.setAttribute("roomTypes", roomTypes);
	     List<Roomimages>images=RoomimagesDAO.getRoomimagesdao().findImagesByRoomTypeId(roomType);
	     ArrayList<RoomTypeComment>roomTypeComments=RoomTypeCommentDAO.getRoomtypecommentdao().getfindall(roomType);
	     int sum=RoomTypeCommentDAO.getRoomtypecommentdao().getsum(roomType);
		request.setAttribute("roomType", roomType);
		request.setAttribute("sum", sum);
		request.setAttribute("roomTypeComments", roomTypeComments);
		request.setAttribute("images", images);
		request.setAttribute("startTime", startTime);
		request.setAttribute("endTime", endTime);
		request.getRequestDispatcher("/FrontDesk/center/comment.jsp").forward(request, response);
	
	}
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
